<template>
	<view>
		<cu-custom bgColor="linear-gradient(to right, rgba(0,0,0,0.7), rgba(0,0,0,0.6))" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">我的日记</block>
		</cu-custom>
		<view class="cu-form-group">
			<view class="title">是否编辑</view>
			<switch class='switch-sex' @change="SwitchC" :class="switchC?'checked':''" :checked="switchC?true:false"></switch>
		</view>
		<form @submit="formSubmit" @reset="formReset">
			<view class="cu-form-group margin-top">
				<view class="title">日记标题</view>
				<input placeholder="请输入日记标题" v-model="diaryInfo.title" :disabled="!switchC"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">日记标签</view>
				<input placeholder="日记标签请以',' 分割" v-model="diaryInfo.tags" :disabled="!switchC"></input>
			</view>
			
			<view class="cu-form-group align-start">
				<view class="title">我想记下：</view>
				<textarea maxlength="-1" v-model="diaryInfo.content" placeholder="多行文本输入框"></textarea>
			</view>
			<view class="uni-btn-v" style="text-align: center;">
				<button class="cu-btn round bg-purple" form-type="submit" :disabled="!switchC">Submit</button>
				<button class="cu-btn round bg-purple" type="default" form-type="reset" :disabled="!switchC">Reset</button>
			</view>
		</form>
	</view>
</template>

<script>
	var _this;
	import {
		saveDiary,
	} from '../../service/api/diary.js' //登录api
	export default {
		data() {
			return {
				diaryInfo:{},
				switchC: false,
			};
		},
		onLoad(){
			const value = uni.getStorageSync('userinfo');
			console.log(value);
			this.diaryInfo.authorId = value.userId;
			_this = this
		},
		methods: {
			SwitchC(e) {
				this.switchC = e.detail.value
			},
			formSubmit: function(e) {
			                console.log('form发生了submit事件，携带数据为：' + JSON.stringify(this.diaryInfo))
			                var formdata = this.diaryInfo
			                saveDiary(formdata)
			                .then(res => {
			                	console.log(res) 
			                	//简单验证下登录（不安全）
			                	if(res.data.code==0){
			                		uni.reLaunch({
			                			url: '../index?PageCur=home',
			                		});
			                	}else{
			                	}
			                	uni.hideLoading();
			                }).catch(err => {
			                	console.log(err)
			                	let msg = err.errMsg; //错误信息
			                	uni.showToast({
			                	    icon: 'none',
			                		position: 'center',
			                	    title: '网络开小差了，小主稍后再试哦^.^'
			                	});
			                	uni.hideLoading();
							})
			},
			formReset: function(e) {
				console.log('清空数据')
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
